<?php
include FCPATH . 'application/controllers/backend/backend.php';

class Album extends Backend {

    public function __construct() {
        parent::__construct();
    }

    public function index() {
        $this->load->model('photo_model');
        $this->load->model('album_model');
        
        $data = array();
        $this->load_javascripts('backend_album');
        $this->rend('backend/album_index_view', $data);
    }

    public function find() {
        $this->load->model('album_model');
        $id = intval($this->input->get('id'));
        $data = $this->album_model->find($id);
        if ($data) {
            $this->json_back('XT001', '', $data);
        } else {
            $this->json_back('XT002', '数据库错误');
        }
    }

    public function records() {
        $this->load->model('album_model');

        $page = intval($this->input->get('page'));
        $page_size = intval($this->input->get('pageSize'));
        $offset = ($page - 1) * $page_size;

        $this->json_back('XT001', '', array(
            'total' => $this->album_model->total(),
            'records' => $this->album_model->records($page_size, $offset)
        ));
    }

    public function insert() {
        $this->load->model('album_model');

        $data = array(
            'used' => 1,
            'cover' => 0,
            'name' => trim($this->input->post('name')),
            'description' => $this->input->post('description'),
            'creater' => intval($this->session->userdata('user_id')),
            'created_at' => date('Y-m-d H:i:s', time())
        );

        if (empty($data['name'])) {
            $this->json_back('XT002', '相册名称不能为空');
        }

        $conditions = array(
            array('name',$data['name'])
        );

        if ($this->album_model->total($conditions) > 0) {
            $this->json_back('XT002', '该相册名称已经存在，请更换一个');
        }
        
        if ($this->album_model->insert($data)) {
            $this->json_back('XT001');
        } else {
            $this->json_back('XT002', 'database error.');
        }
    }

    public function update() {
        $this->load->model('album_model');
        $id = intval($this->input->post('id'));

        $data = array(
            'name' => trim($this->input->post('name')),
            'description' => $this->input->post('description'),
        );
        
        if (empty($data['name'])) {
            $this->json_back('XT002', '相册名称不能为空');
        }
        
        $conditions = array(
            array('name',$data['name']),
            array('id <>',$id)
        );

        if ($this->album_model->total($conditions) > 0) {
            $this->json_back('XT002', '该相册名称已经存在，请更换一个');
        }

        if ($this->album_model->update($id, $data)) {
            $this->json_back('XT001');
        } else {
            $this->json_back('XT002', 'database error.');
        }
    }

    public function delete() {
        $this->load->model('album_model');
        $ids = explode(',', $this->input->get('id'));
        if ($this->album_model->delete($ids)) {
            $this->json_back('XT001');
        } else {
            $this->json_back('XT002', 'database error.');
        }
    }
}
